package com.example.ui.contact.adapter;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.service.MainService;
import com.example.ui.contact.PersonHomeActivity;
import com.example.ui.contact.bean.User;
import com.example.ui.contact.chat.ApiKeyUtil;
import com.example.ui.contact.chat.ChatActivity;
import com.example.ui.contact.chat.L;
import com.example.ui.leftmenu.Item08View;
import com.example.util.Util;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 联系人列表适配器。
 * 
 * @author Leone
 */
 public class ContactsListViewAdapter extends BaseAdapter {
	
	public Context context;  
    public List<User> list;

	/**
	 * 字母表分组工具
	 */
	private SectionIndexer mIndexer;
	
	public static final int INFO_MSG = 0;
	public static final int TEL_MSG = 1;
	public static final int CHAT_MSG = 2;

	public  int curIndex;
	private User curUser;
	private LayoutInflater inflater;
	public ContactsListViewAdapter(Context context, List<User> list) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		L.Debug("ContactsListViewAdapter size2", ""+list.size());
		
		Holder holder = null;
		convertView=null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.chat_contact_item, null);
			holder = new Holder();
			holder.name = (TextView)convertView.findViewById(R.id.name);
			holder.sortKeyLayout = (LinearLayout)convertView.findViewById(R.id.sort_key_layout);
			holder.head_img = (ImageView)convertView.findViewById(R.id.head_img);
			holder.tel_img = (ImageView)convertView.findViewById(R.id.tel_img);
			holder.msg_img = (ImageView)convertView.findViewById(R.id.msg_img);			
        	convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }


	
	    User user = list.get(position);		

		
		String nick = user.getNick();
		holder.name.setText(nick);

		if(MainService.isLeftMenu==true){
			holder.head_img.setEnabled(false);
			holder.head_img.setClickable(false);
		}else{
			holder.head_img.setEnabled(true);
			holder.head_img.setClickable(true);
			
		}
		
		holder.head_img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				Util.toast(context, "head_img onclick"+curIndex);
				Message handlerMsg = mainHandler.obtainMessage(INFO_MSG);
				handlerMsg.obj = mainHandler;
				mainHandler.sendMessage(handlerMsg);

			}			
		});
		
		holder.tel_img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				curUser = list.get(position);
				Util.toast(context, "tel_img onclick"+position);
				//				Message handlerMsg = mainHandler.obtainMessage(INFO_MSG);
				//				handlerMsg.obj = mainHandler;
				//				mainHandler.sendMessage(handlerMsg);
				
				Uri telUri = Uri.parse("tel:"+curUser.getTelNum()); //Uri.parse("tel:"+Item08View.contacts.get(position).getNum());
				Intent returnIt = new Intent(Intent.ACTION_DIAL, telUri);
				context.startActivity(returnIt);
			}
			
		});
		holder.msg_img.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				curIndex = position;
				curUser = list.get(position);
				Util.toast(context, "msg_img onclick"+curIndex);
				Message handlerMsg = mainHandler.obtainMessage(CHAT_MSG);
				handlerMsg.obj = mainHandler;
				mainHandler.sendMessage(handlerMsg);
				//				PushManager.startWork(context.getApplicationContext(),
				//						PushConstants.LOGIN_TYPE_API_KEY,
				//						ApiKeyUtil.getMetaValue(context, "api_key"));// 无baidu帐号登录
				//				Intent chatIntent = new Intent(context,ChatActivity.class);
				//				context.startActivity(chatIntent);
			}
			
		});
		
	
		return convertView;
	}
	
	
	/**
	 * 给当前适配器传入一个分组工具。
	 * 
	 * @param indexer
	 */
	public void setIndexer(SectionIndexer indexer) {
		mIndexer = indexer;
	}
	

	Handler mainHandler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case INFO_MSG:
				Intent intent = new Intent(context,PersonHomeActivity.class);
				context.startActivity(intent);
				break;
			case TEL_MSG:
				Uri telUri = Uri.parse("tel:"+curUser.getTelNum());
				Intent returnIt = new Intent(Intent.ACTION_DIAL, telUri);
				context.startActivity(returnIt);				
				break;
			case CHAT_MSG:
				//				PushManager.startWork(context.getApplicationContext(),
				//				PushConstants.LOGIN_TYPE_API_KEY,
				//				ApiKeyUtil.getMetaValue(context, "api_key"));// 无baidu帐号登录
				
				L.Debug("curUser.getNick()=", curUser.getNick());
				L.Debug("curUser.getId()=", curUser.getId());
				
		        Intent chatIntent = new Intent(context,ChatActivity.class);
		        chatIntent.putExtra("mName", curUser.getNick());
		        chatIntent.putExtra("mUserId", curUser.getId());//curUser.getUserId()
		        context.startActivity(chatIntent);
				break;
			
			default:
				break;
			}
		};
	};

}

	class Holder{
		LinearLayout layout;
		TextView name;
		LinearLayout sortKeyLayout;
		TextView sortKey ;
		ImageView head_img ;
		ImageView tel_img ;
		ImageView msg_img ;
 } 
 
